import { evalEl } from './auth'
import request from '@/utils/request'
import { getRemoteUrlParam, getFnInputParam } from '@/utils/hyurl'

export function initEnumParam(thisObj) {
  if (thisObj.initUiVal !== undefined && thisObj.initUiVal.enumList !== undefined) {
    thisObj.enumParam = Object.assign({}, thisObj.initUiVal.enumList)
  }
}

export function changeFormEnumKey(thisObj, record) {
  const enumKey = thisObj.formList.enumKey
  const formEnumData = getActiveCommonEnumData(thisObj, record, enumKey, thisObj.initUiVal.formList.enumData)
  // thisObj.activeEnumData = Object.assign({}, formEnumData)
  let activeListEnumData = {}
  const listFilterKey = thisObj.activeFilters
  for (const i in listFilterKey) {
    const S_RULE_TPL_KEY = listFilterKey[i]
    const enumList = thisObj.columnFiltersData[S_RULE_TPL_KEY]
    const enumObj = getActiveEnumObj(thisObj, record, enumList)
    if (enumObj === null || enumObj === undefined) {
      continue
    }
    thisObj.activeColumnFiltersData[S_RULE_TPL_KEY] = enumObj
    const tmpKeyList = []
    tmpKeyList.push(enumObj.model)
    const tmpKeyEnumData = getActiveCommonEnumData(thisObj, record, tmpKeyList, thisObj.columnFiltersData)
    activeListEnumData = Object.assign({}, activeListEnumData, tmpKeyEnumData)
    for (const j in thisObj.listColActiveList) {
      const columnData = thisObj.listColActiveList[j]
      const dataIndex = columnData.dataIndex
      if (S_RULE_TPL_KEY !== dataIndex) {
        continue
      }
      columnData.filters = activeListEnumData[enumObj.model]
    }
  }
  thisObj.activeEnumData = Object.assign({}, formEnumData, activeListEnumData)
}

export function getActiveCommonEnumData(thisObj, record, enumKeyList, enumData) {
  const cccc = {}
  for (const i in enumKeyList) {
    const S_RULE_TPL_KEY = enumKeyList[i]
    const enumList = enumData[S_RULE_TPL_KEY]
    const tmpObj = changeActiveCommonEnumData(thisObj, S_RULE_TPL_KEY, record, enumList)
    if (tmpObj !== null) {
      cccc[S_RULE_TPL_KEY] = tmpObj[S_RULE_TPL_KEY]
    }
  }
  return cccc
}

export function changeActiveEnumKey(thisObj, S_RULE_TPL_KEY, record) {
  const cccc = {}
  const tmpObj = changeActiveEnumData(thisObj, S_RULE_TPL_KEY, record)
  if (tmpObj !== null) {
    cccc[S_RULE_TPL_KEY] = tmpObj[S_RULE_TPL_KEY]
  }
  thisObj.activeEnumData = Object.assign({}, thisObj.activeEnumData, cccc)
  thisObj.globalData.activeEnumData = thisObj.activeEnumData
}

export function changeActiveListEnumData(thisObj, S_RULE_TPL_KEY, record) {
  const enumList = thisObj.columnFiltersData[S_RULE_TPL_KEY]
  return changeActiveCommonEnumData(thisObj, S_RULE_TPL_KEY, record, enumList)
}

export function changeActiveEnumData(thisObj, S_RULE_TPL_KEY, record) {
  const enumList = thisObj.initUiVal.formList.enumData[S_RULE_TPL_KEY]
  return changeActiveCommonEnumData(thisObj, S_RULE_TPL_KEY, record, enumList)
}

export function getActiveEnumObj(thisObj, record, enumList) {
  for (const e in enumList) {
    const enumTmp = enumList[e]
    if (evalEl(enumTmp.objEl, record)) {
      return enumTmp
    }
  }
  return null
}

export function changeActiveCommonEnumData(thisObj, S_RULE_TPL_KEY, record, enumList) {
  const enumTmp = getActiveEnumObj(thisObj, record, enumList)
  if (enumTmp !== null) {
    const tmpObj = {}
    const data = enumTmp.data
    const tmpList = thisObj.enumData[data]
    if (tmpList !== undefined) {
      tmpObj[S_RULE_TPL_KEY] = tmpList
    } else {
      tmpObj[S_RULE_TPL_KEY] = []
    }
    return tmpObj
  }
  return null
}

export function changeLoadEnumList(thisObj, triggerObj, record) {
  const S_RULE_TPL_KEY = triggerObj.s_RULE_TPL_KEY
  const enumList = thisObj.initUiVal.formList.enumData[S_RULE_TPL_KEY]
  let urlParam = {}
  for (const e in enumList) {
    const enumTmp = enumList[e]
    if (evalEl(enumTmp.objEl, thisObj.globalData.globalParam)) {
      const urlKey = 'enum'
      const busiParam = enumTmp.busiParam

      if (busiParam !== undefined && busiParam.length > 0) {
        console.log('配置枚举业务参数.', busiParam)
        urlParam = getFnInputParam(thisObj, thisObj.globalData, busiParam)
      }
      let param = getRemoteUrlParam(thisObj, urlKey, thisObj.globalData)
      param = Object.assign({}, param, thisObj.enumParam, urlParam)
      param.loadEnumBusiPK = enumTmp.busiPK
      thisObj.loading = true
      getEnumData(param).then((res) => {
        thisObj.loading = false
        const enumData = res.data.enumData
        thisObj.enumData = Object.assign({}, thisObj.enumData, enumData)
        changeActiveEnumKey(thisObj, S_RULE_TPL_KEY, record)
      })
      break
    }
  }
}

export function getEnumBusiParam(thisObj, enumKey, globalData) {
  const enumData = thisObj.initUiVal.formList.enumData
  let urlParam = {}
  const enumList = enumData[enumKey]
  for (const e in enumList) {
    const enumTmp = enumList[e]
    if (evalEl(enumTmp.objEl, globalData.globalParam)) {
      const busiParam = enumTmp.busiParam
      if (busiParam !== undefined && busiParam.length > 0) {
        urlParam = getFnInputParam(thisObj, thisObj.globalData, busiParam)
      }
      return urlParam
    }
  }
  return urlParam
}

export function initEnum(thisObj, globalData) {
  const enumData = thisObj.initUiVal.formList.enumData
  let allEnumParam = {}
  for (const key in enumData) {
    allEnumParam = Object.assign({}, allEnumParam, getEnumBusiParam(thisObj, key, thisObj.globalData))
  }
  const urlKey = 'enum'
  // const tmpParam = Object.assign({}, thisObj.initData, record)
  let extParam = {}
  if (thisObj.dealParam[urlKey] !== undefined && thisObj.dealParam[urlKey].S_DY_PARAM !== undefined) {
    extParam = getRemoteUrlParam(thisObj, urlKey, thisObj.globalData)
  }
  const param = Object.assign({}, extParam, thisObj.enumParam, allEnumParam)

  const dataCompKey = thisObj.initUiVal.formList.dataCompKey
  const tplCompKey = thisObj.initUiVal.formList.tplCompKey
  if (dataCompKey !== '' || tplCompKey !== '') {
    param.S_PAGE_ID_IDX = thisObj.uiPageUrl.S_UI_PAGE_ID_IDX_2025
    param.S_PAGE_VER_IDX = thisObj.uiPageUrl.S_UI_PAGE_VER_IDX_2025
    param.S_PANEL_TPL_ID = thisObj.initLoadObj.loadPanelId
    param.S_PANEL_TPL_VER = thisObj.initLoadObj.loadPanelVer
  }
  thisObj.loading = true
  getEnumData(param).then((res) => {
    thisObj.loading = false
    thisObj.enumData = res.data.enumData
    changeFormEnumKey(thisObj, thisObj.initData)
  })
}

export function getEnumData(parameter) {
  console.log('getEnumData.....', parameter)
  let url = ''
  if (parameter.url === null || undefined === parameter.url || parameter.url === '/run/ctr/submitJson') {
    url = '/enum/ctr/getEnumList'
  } else {
    url = parameter.url
  }
  const newurl = '/autodeal' + url
  return request.postJson(newurl, parameter)
}
